package com.tobe.spring_demo.xmlstart;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author yangqifan 2015年9月10日
 * 
 */
public class XmlStyleDemoTest {
	
	public static void main(String[] args) {
		//appcontext, spring容器上下文,解析xml配置创建bean容器, 
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
		//参数可以是类也可以是接口,返回结果有多个会抛出异常,比如传入接口  org.springframework.beans.factory.NoUniqueBeanDefinitionException
		//MarkService service = context.getBean(MarkService.class);
		MyService service = context.getBean(MyService.class);//
		service.select();
		System.out.println(context.getBean(SimpleBean.class).getName());
		
		
		//FactroyBean测试, 可以通过facory id获取,也可以通过factory产出的类获取
		Apple bean1 = (Apple) context.getBean("myAppleFactory");//factorybean myAppleFactory 和 Apple.class 都可以作为key
		Apple bean = context.getBean(Apple.class);
		Apple bean2 = (Apple) context.getBean("myAppleFactory");
		Apple bean3 = (Apple) context.getBean("myAppleFactory");
		Apple bean4 = context.getBean(Apple.class);
		
		System.out.println(bean1);
		System.out.println(bean);
		System.out.println(bean2);
		System.out.println(bean3);
		System.out.println(bean4);
		
		//初始化方法
		//1.实现InitializingBean,会回调afterSetProperties方法
		//2.xmlbean配置中设置init-method方法
		//3.在<beans default-init-method="init"></beans> 中间所有的bean使用init作为初始化方法
		//4.使用@PostConstruct
		//几个地方定义会依次执行
		
		//事务
		
	}

}

